//
//  KZMessageOfMineView.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/27.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZMessageOfMineView: UITableView {
    
    var emptyView: KZMessageEmptyView! = nil
    
    var items1: [String] = []
    var items2: [String] = []
    
    var items: [[String]] = [[], []] {
        didSet {
            self.reloadData()
            items1 = items.first!
            items2 = items.last!
            if items1.count == 0, items2.count == 0 {
                self.backgroundView = emptyView
            } else {
                self.backgroundView = UIView()
            }
        }
    }
    convenience init() {
        self.init(frame: .zero, style: .plain)
        
        setupUI()
    }
    

}
extension KZMessageOfMineView {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if emptyView == nil {
            emptyView = KZMessageEmptyView(frame: self.bounds, image: #imageLiteral(resourceName: "消息-空"), text: "当前没有消息")
        }
    }
    
    func setupUI() {
        
        self.backgroundColor = kBgColor
        self.estimatedRowHeight = 200
        self.rowHeight = UITableViewAutomaticDimension
        self.separatorStyle = .none
        self.tableFooterView = UIView()
        self.delegate = self
        self.dataSource = self
  
        self.register(KZMessageOfMineCell.self, forCellReuseIdentifier: "cell")
    }
}


// MARK: ------ tableview datasource
extension KZMessageOfMineView: UITableViewDelegate, UITableViewDataSource {
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return items[section].count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell: KZMessageOfMineCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! KZMessageOfMineCell
        return cell
    }
   
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 {
           
            return items2.count == 0 ? 0 : 30
        }
        return 10
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 1 {
            
            let header = UIView()
            header.backgroundColor = UIColor.white
            
            let label = UILabel()
            label.textColor = UIColor.lightGray
            label.textAlignment = .center
            label.text = "2周前的消息"
            label.font = UIFont.systemFont(ofSize: 12)
            header.addSubview(label)
            label.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 30)
            
            let line = UIView()
            line.backgroundColor = kLineColor
            line.frame = CGRect(x: 0, y: 29.5, width: kScreenWidth, height: 0.5)
            header.addSubview(line)
            
            return items2.count == 0 ? UIView() : header
        }
        
        return UIView()
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
